package com.lingkang.top.seesister.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lingkang.top.seesister.R;
import com.lingkang.top.seesister.activity.home.fragment.ErCiYuanFragment;
import com.lingkang.top.seesister.activity.home.fragment.MessageFragment;
import com.lingkang.top.seesister.activity.home.fragment.UserFragment;
import com.lingkang.top.seesister.entity.TabEntity;
import com.lingkang.top.seesister.utils.ViewFindUtils;

import java.util.ArrayList;
import java.util.Random;

public class HomeActivity extends AppCompatActivity {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<Fragment> mFragments2 = new ArrayList<>();
    private String[] mTitles = {"二次元", "消息", "我"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private View mDecorView;
    private CommonTabLayout mTabLayout_2;
    private ArrayList<Fragment> pageFragment = new ArrayList<>();

    private FragmentManager supportFragmentManager;
    private FragmentTransaction fragmentTransaction;
    private static int thisIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        for (String title : mTitles) {
            mFragments.add(SimpleCardFragment.getInstance("Switch ViewPager " + title));
            mFragments2.add(SimpleCardFragment.getInstance("Switch Fragment " + title));
        }

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        mDecorView = getWindow().getDecorView();
        /** with ViewPager */
        mTabLayout_2 = ViewFindUtils.find(mDecorView, R.id.tl_2);
        initBar();

        //两位数
//        mTabLayout_2.showMsg(0, 55);
//        mTabLayout_2.setMsgMargin(0, -5, 5);

//        //三位数
//        mTabLayout_2.showMsg(1, 100);
//        mTabLayout_2.setMsgMargin(1, -5, 5);
//
//        //设置未读消息红点
//        mTabLayout_2.showDot(2);
//        MsgView rtv_2_2 = mTabLayout_2.getMsgView(2);
//        if (rtv_2_2 != null) {
//            UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f));
//        }
//
//        //设置未读消息背景
//        mTabLayout_2.showMsg(3, 5);
//        mTabLayout_2.setMsgMargin(3, 0, 5);
//        MsgView rtv_2_3 = mTabLayout_2.getMsgView(3);
//        if (rtv_2_3 != null) {
//            rtv_2_3.setBackgroundColor(Color.parseColor("#6D8FB0"));
//        }
        initFrament();
    }

    private void initFrament() {
        //添加fragment页面
        pageFragment.add(new ErCiYuanFragment());
        pageFragment.add(new MessageFragment());
        pageFragment.add(new UserFragment());
        //获取管理者
        supportFragmentManager = getSupportFragmentManager();
        //开启事务
        fragmentTransaction = supportFragmentManager.beginTransaction();
        //默认进入二次元
        fragmentTransaction.add(R.id.fl_home, pageFragment.get(thisIndex)).commit();
        Log.i("Button", "the button is clicked");
    }

    Random mRandom = new Random();

    private void initBar() {
        mTabLayout_2.setTabData(mTabEntities);
        mTabLayout_2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                //获取管理者
                supportFragmentManager = getSupportFragmentManager();
                //开启事务
                fragmentTransaction = supportFragmentManager.beginTransaction();
                if (thisIndex != position) {
                    //先隐藏 再添加新的或显示已经添加的
                    if (pageFragment.get(position).isAdded()) {
                        fragmentTransaction.hide(pageFragment.get(thisIndex)).show(pageFragment.get(position)).commit();
                    } else {
                        fragmentTransaction.hide(pageFragment.get(thisIndex)).add(R.id.fl_home, pageFragment.get(position)).commit();
                    }
                    thisIndex = position;
                }
            }

            @Override
            public void onTabReselect(int position) {
                if (position == 0) {
                    mTabLayout_2.showMsg(0, mRandom.nextInt(100) + 1);
                }
            }
        });
    }

    private void changeFragment(Fragment fromFragment, Fragment toFragment) {

    }
}
